package getquestion

import (
	"database/sql"
	"strconv"

	"github.com/gin-gonic/gin"
)

func Sper(r *gin.Engine) {
	var db *sql.DB
	var err error

	db, err = sql.Open("postgres", "port=5432 user=postgres password=15566840887 dbname=mymbri sslmode=disable")
	if err != nil {
		panic(err)
	}

	// 获取搜索
	r.GET("/getper", func(c *gin.Context) {
		xx := c.Query("xx")
		var per, per2 string = "0", "0"
		str := "SELECT count(*) FROM testlog group by mal having mal='" + xx + "'"
		rows, err := db.Query(str)
		if err != nil {
			panic(err)
		}

		for rows.Next() {

			err = rows.Scan(&per)
			if err != nil {
				panic(err)
			}

		}

		//基数
		str2 := "SELECT count(*)FROM testlog"
		rows2, err := db.Query(str2)
		if err != nil {
			panic(err)
		}

		for rows2.Next() {
			err = rows2.Scan(&per2)
			if err != nil {
				panic(err)
			}
		}
		var int3 float64
		int1, err := strconv.ParseFloat(per, 64)
		int2, err := strconv.ParseFloat(per2, 64)
		int3 = (int1 / int2) * 100
		c.JSON(200, gin.H{
			"per":  int3,
			"mal":  xx,
			"int1": int1,
			"int2": int2,
		})
	})
}
